"""文本清洗模块：负责清洗和标准化问题与选项文本"""

import re
import pandas as pd
from typing import Union, List
from .config import (
    TERM_STANDARDIZATION,
    SPECIAL_CHARACTERS_PATTERNS,
    REDUNDANT_TEXT_PATTERNS,
    OPTION_PREFIX_PATTERN
)


class TextCleaner:
    """文本清洗器：处理问题和选项中的噪声、标准化术语"""

    @staticmethod
    def clean_general_text(text: Union[str, float, None]) -> str:
        """
        通用文本清洗：处理特殊符号、冗余信息和标准化

        Args:
            text: 原始文本

        Returns:
            清洗后的文本
        """
        if pd.isna(text):
            return ""

        # 转换为字符串并去除首尾空格
        text = str(text).strip()

        # 处理特殊符号和空格
        for pattern, replacement in SPECIAL_CHARACTERS_PATTERNS.items():
            text = re.sub(pattern, replacement, text)

        # 去除冗余文本
        for pattern in REDUNDANT_TEXT_PATTERNS:
            text = re.sub(pattern, '', text)

        # 术语标准化
        for pattern, replacement in TERM_STANDARDIZATION.items():
            text = re.sub(pattern, replacement, text)

        return text

    @staticmethod
    def clean_option_text(option: Union[str, float, None]) -> str:
        """
        选项文本清洗：在通用清洗基础上额外去除选项前缀

        Args:
            option: 原始选项文本

        Returns:
            清洗后的选项内容
        """
        # 先进行通用清洗
        cleaned_option = TextCleaner.clean_general_text(option)

        # 去除选项前缀（如"A: "、"B. "、"C、"等）
        cleaned_option = re.sub(OPTION_PREFIX_PATTERN, '', cleaned_option)

        return cleaned_option
